package lightswitch.server.code.object;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Reference {
    private String name;
    private String field;
    private String fieldRef;
    private String tableRef;
    
    public Reference(){}
    public Reference(String name, String field, String fieldRef, String tableRef){
        this.name = name.toLowerCase();
        this.field = field.toLowerCase();
        this.fieldRef = fieldRef.toLowerCase();
        this.tableRef = tableRef.toLowerCase();
    }
    
    public String getName(){
        return name;
    }
    
    public String getField(){
        return field;
    }
    
    public String getFieldRef(){
        return fieldRef;
    }
    
    public String getTableRef(){
        return tableRef;
    }
    
    public void setName(String value){
        name = value.toLowerCase();
    }
    
    public void setField(String value){
        field = value.toLowerCase();
    }
    
    public void setFieldRef(String value){
        fieldRef = value.toLowerCase();
    }
    
    public void setTableRef(String value){
        tableRef = value.toLowerCase();
    }
    
    public static Element createElement(Document doc, Reference ref){
        Element result = doc.createElement("REFERENCE");
        result.setAttribute("name", ref.getName());
        result.setAttribute("field", ref.getField());
        result.setAttribute("fieldRef", ref.getFieldRef());
        result.setAttribute("tableRef", ref.getTableRef());
        return result;
    }
    
    public static Reference createReferenceFromNode(Element referenceElement){
        String name = referenceElement.getAttribute("name").toLowerCase();
        String field = referenceElement.getAttribute("field").toLowerCase();
        String tableRef = referenceElement.getAttribute("tableRef").toLowerCase();
        String fieldRef = referenceElement.getAttribute("fieldRef").toLowerCase();
        return new Reference(name, field, fieldRef, tableRef);
    }
    public String getString() {
        String result = "";
        result += getName() + ":    ";
        result += getField() + " ref to ";
        result += getFieldRef()+" in ";
        result += getTableRef();
        return result;
    }
}

